<?php
/**
 * Created by PhpStorm.
 * User: dev
 * Date: 2018/7/26
 * Time: 13:54
 */
namespace app\home\controller;

use \app\common\controller\Frontend;
use think\Db;
use think\Validate;

class Index extends Frontend
{
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = '*';
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = '*';

    protected $res = [];

    //cam发行总量
    protected $cam = 25000000;

    public function _initialize()
    {
        parent::_initialize();
    }

    /*
     * CTS首頁
     */
    public function index()
    {
        $total = Db::name('asset_exchange')->where('type',3)->sum('cts_exchange');
        $award = Db::name('award_plan')->where('status','normal')->order('id')->select();
        $config = Db::name('currency_config')->where('status','normal')->find();
        $start_Md = date('M.d',$config['starttime']);
        $end_Md = date('M.d',$config['endtime']);
        $start_Hi = date('H:i a',$config['starttime']);
        $end_Hi = date('H:i a',$config['endtime']);
        $config['start_Md'] = $start_Md;
        $config['end_Md'] = $end_Md;
        $config['start_Hi'] = $start_Hi;
        $config['end_Hi'] = $end_Hi;
        $val = json_encode($config);
        $plan = json_encode($award);
        $this->assign('list',$config);
        $this->assign('val',$val);
        $this->assign('award',$award);
        $this->assign('plan',$plan);
        $this->assign('total',$total);
        //true:手机端;false:游览器
        $flag = isMobile();
        if( $flag ) {
            return $this->view->fetch('index/index_mobile');
        } else {
            return $this->view->fetch();
        }
    }

    /**
     * 兑换提交
     * @return \think\response\Json
     */
    public function assetExchange(){
        $total = Db::name('asset_exchange')->where('type',3)->sum('cts_exchange');
        $tips = $this->cam - $total;
        if($this->request->isAjax()){
            $time  = time();
            $data = input();
            $data['refreshtime'] = $time;
            $data['completetime'] = $time;
            $rule = [
                'cts_address'   => 'require',
                'type_address'  => 'require',
                'type_num'      => 'require|number'
            ];
            $msg = [
                'cts_address.require'  => '收款地址不能为空',
                'type_address.require' => '付款地址不能为空',
                'type_num.require'     => '币种数量不能为空',
                'type_num.number'     => '币种数量只能为数字'
            ];
            $da = [
                'cts_address'   => $data['cts_address'],
                'type_address'  => $data['type_address'],
                'type_num'  => $data['type_num']
            ];
            $validate = new Validate($rule, $msg);
            $result = $validate->check($da);

            if ( !$result ) {
                $this->error(__($validate->getError()), null);
                return false;
            }
            //cam兑换cts
            if($data['type']==3){
                if($data['cts_exchange']>$total){
                    $res = array(
                        'code' => 0,
                        'msg'    => '本轮cam兑换总量不超过'.$this->cam
                    );
                    return json($res);
                }
            }
            $insert = Db::name('asset_exchange')->insert($data);
            if($insert){
                $res = array(
                    'code' => 1,
                    'msg'    => '兑换成功'
                );
                return json($res);
            }else{
                $res = array(
                    'code' => 0,
                    'msg'    => '兑换失败'
                );
                return json($res);
            }
        }else{
            $res = array(
                'code' => 0,
                'msg'    => '非法请求'
            );
            return json($res);
        }
    }
}